package org.example.hrpc.rpc.client;

import io.netty.channel.*;
import org.example.hrpc.rpc.core.Result;
import org.example.hrpc.rpc.message.MessageType;
import org.example.hrpc.rpc.message.Request;
import org.example.hrpc.rpc.message.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransportClientHandler extends SimpleChannelInboundHandler<Response> {
    private static final Logger logger = LoggerFactory.getLogger(TransportClientHandler.class);

    /**
     * 用户线程发送完请求后的动作，会调用DefaultFuture.get()方法等待响应对象的到来
     * 当响应对象到来后，用户线程会被唤醒，并通过调用编号获取属于自己的响应对象
     * 通过DefaultFuture.received方法会将响应对象保存到相应的DefaultFuture实例中，，
     * @param channelHandlerContext
     * @param response
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Response response) throws Exception {
        if(response != null) {
            logger.info("Receive Response Message, status: {}, result: {}", response.getServiceStatus(), response.getResult());
            DefaultFuture.received(channelHandlerContext.channel(), response);
        }
    }
}
